#ifndef VTKWRITER_HPP
#define VTKWRITER_HPP

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "Types.hpp"
#include <vector>
#include <iostream>
#include <stdlib.h>
#include "Vertex.hpp"
#include <map>

class VTKWriter
{
public:

    VTKWriter( const std::string &problem)
    {

        std::string filename = problem;
        filename += ".vtk";
        problem1 = filename;
        mStream = fopen(filename.c_str(), "w");

        if ( mStream == NULL )
        {
            std::cout <<"Error opening outputfile";
        }

        fprintf( mStream, "# vtk DataFile Version 4.0\n");
        fprintf( mStream, "Siwir2 %s\n",problem.c_str());
        fprintf( mStream, "ASCII\n");
        fprintf( mStream, "DATASET UNSTRUCTURED_GRID\n");
    }

    ~VTKWriter( )
    {
        fprintf( mStream, "\n" );
        fclose(mStream);
    }


    void WriteData(const std::map<size_t,Vertex> &vertex, const std::vector<real> &u)
    {
        fprintf(mStream,"POINTS %ld double\n",vertex.size());

        for(auto it = vertex.begin(); it!=vertex.end(); ++it )
        {
            fprintf(mStream,"%g  %g  0\n",(*it).second.getCoordinate(0),(*it).second.getCoordinate(1) );
        }
        fprintf(mStream,"\nPOINT_DATA %ld\n\n",u.size());
        fprintf( mStream, "SCALARS u_field double\n" );

        fprintf(mStream,"\nLOOKUP_TABLE default \n");

        for(auto it = u.begin(); it!=u.end(); ++it )
        {
            fprintf(mStream,"%g \n",(*it));
        }
    }



private:

    FILE *mStream;
    //unsigned int mDataCount;
    std::string problem1;
};
#endif // VTKWRITER_HPP




